﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Nova_MOD;

namespace Nova_DAL
{
    public class Bill
    {
        int i = 0;
        string strSQL = "";
        DataSet ds = null;
        DbHelper dbHelper = new DbHelper();
        DbCommand cmd;


        /// <summary>
        /// 获取到入库订单信息实体
        /// </summary>
        /// <param name="StockNo"></param>
        /// <param name="billInfor"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:失败  0：没有获取到  1：成功</returns>
        public int AddBillInfor(string StockNo, out BillInfor billInfor, out string strMessage)
        {
            strMessage = "";
            ds =new DataSet();
            billInfor = new BillInfor();
            billInfor.BillDitails = new List<BillDitail>();
            try
            {
                //获取入库单主表信息
                strSQL = string.Format(@"select * from TBill_AddStorage where StockNo = @StockNo");
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
                ds = dbHelper.ExecuteDataSet(cmd);

                if (ds.Tables[0].Rows.Count <= 0)
                {
                    strMessage = "没有获取到物料信息";
                    return 0;


                }

                billInfor.StockNo = StockNo;
                billInfor.Small_BillType = ds.Tables[0].Rows[0]["Small_BillType"].ToString();
                billInfor.Contents = ds.Tables[0].Rows[0]["Contents"].ToString();

                //获取主库单明细信息
                strSQL = string.Format(@"select * from TAddStorage_List where StockNo = @StockNo");
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
                ds = new DataSet();
                ds = dbHelper.ExecuteDataSet(cmd);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        BillDitail item = new BillDitail();
                        item.Mat_Code = ds.Tables[0].Rows[i]["Mat_Code"].ToString();
                        item.Num = Convert.ToInt32(ds.Tables[0].Rows[i]["Num"]);
                        item.Act_Num = Convert.ToInt32(ds.Tables[0].Rows[i]["Act_Num"]);
                        item.TS02 = ds.Tables[0].Rows[i]["TS02"].ToString();
                        item.TS03 = ds.Tables[0].Rows[i]["TS03"].ToString();

                        billInfor.BillDitails.Add(item);
                    }




                }
            }

            catch (Exception ex)
            {
                strMessage = "获取数据异常";
                return -1;
            }

            return 1;
        }


        /// <summary>
        /// 获取出库单信息实体
        /// </summary>
        /// <param name="StockNo"></param>
        /// <param name="billInfor"></param>
        /// <param name="strMessage"></param>
        /// <returns> -1:失败  0：没有获取到  1：成功</returns>
        public int DelBillInfor(string StockNo, out BillInfor billInfor, out string strMessage)
        {
            strMessage = "";
            ds = new DataSet();
            billInfor = new BillInfor();
            billInfor.BillDitails = new List<BillDitail>();
            try
            {
                //获取出库单主表信息
                strSQL = string.Format(@"select * from TBill_DelStorage where StockNo = @StockNo");
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
                ds = dbHelper.ExecuteDataSet(cmd);

                if (ds.Tables[0].Rows.Count <= 0)
                {
                    strMessage = "没有获取到物料信息";
                    return 0;


                }

                billInfor.StockNo = StockNo;
                billInfor.Small_BillType = ds.Tables[0].Rows[0]["Small_BillType"].ToString();
                billInfor.Contents = ds.Tables[0].Rows[0]["Contents"].ToString();

                //获取主出库单明细信息
                strSQL = string.Format(@"select * from TDelStorage_List where StockNo = @StockNo");
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
                ds = new DataSet();
                ds = dbHelper.ExecuteDataSet(cmd);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        BillDitail item = new BillDitail();
                        item.Mat_Code = ds.Tables[0].Rows[i]["Mat_Code"].ToString();
                        item.Num = Convert.ToInt32(ds.Tables[0].Rows[i]["Num"]);
                        item.Act_Num = Convert.ToInt32(ds.Tables[0].Rows[i]["Act_Num"]);
                        item.TS02 = ds.Tables[0].Rows[i]["TS02"].ToString();
                        item.TS03 = ds.Tables[0].Rows[i]["TS03"].ToString();

                        billInfor.BillDitails.Add(item);
                    }




                }
            }

            catch (Exception ex)
            {
                strMessage = "获取数据异常";
                return -1;
            }

            return 1;
        }

        /// <summary>
        /// 更新订单的同步状态
        /// </summary>
        /// <param name="StockNo"></param>
        /// <param name="Type">0：入库单   1：出库单</param>
        /// <param name="Asyn_Status"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败 1：成功</returns>
        public int UpdateStorageAsynStatus(string StockNo, int Type, int Asyn_Status, out string strMessage)
        {
            strMessage = "";
            try
            {
                if (Type == 1)
                {
                    strSQL = string.Format(@"update TBill_DelStorage set Asyn_Status = @Asyn_Status where StockNo = @StockNo ");
                }
                else
                {
                    strSQL = string.Format(@"update TAddStorage_List set Asyn_Status = @Asyn_Status where StockNo = @StockNo ");
                }
              
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                dbHelper.AddInParameter(cmd, "@Asyn_Status", DbType.Int32, Asyn_Status);
                dbHelper.AddInParameter(cmd, "@StockNo", DbType.String, StockNo);
            
                i = dbHelper.ExecuteNonQuery(cmd);
                if (i <= 0)
                {
                    strMessage = "更新订单同步状态失败";
                    return -1;
                }

                return 1;
            }
            catch (Exception ex)
            {
                strMessage = "执行更新订单同步状态异常";
                return -1;
            }
        }


    }
}
